package com.xspace.android.xafcommon.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.xspace.android.xafcommon.R;

/**
 * Wubj 创建于 2017/1/10 0027.
 */
public class EditTextDialog extends Dialog {

    private TextView titleTv;
    private EditText etInput;
    private TextView leftTv;
    private TextView rightTv;
    private ButtonClickListener leftListener;
    private ButtonClickListener rightListener;
    private Context mContext;

    private EditTextDialog(Context context, int theme, String strMessage) {
        super(context, theme);
        mContext = context;
        this.setContentView(R.layout.dialog_edit_text);
        this.setCanceledOnTouchOutside(false);
        findView();
        if (titleTv != null && !TextUtils.isEmpty(strMessage)) {
            titleTv.setText(strMessage);
        }
    }

    private void findView() {
        titleTv = (TextView) findViewById(R.id.txt_title);
        etInput = (EditText) findViewById(R.id.et_input);
        leftTv = (TextView) findViewById(R.id.txt_left);
        rightTv = (TextView) findViewById(R.id.txt_right);
    }

    public void setLeftListener(ButtonClickListener listener) {
        this.leftListener = listener;
    }

    public void setRightListener(ButtonClickListener listener) {
        this.rightListener = listener;
    }

    public void setListener() {
        leftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (leftListener != null) {
                    leftListener.buttonClick(v);
                }
                dismiss();
            }
        });
        rightTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rightListener != null) {
                    rightListener.buttonClick(v);
                }
                dismiss();
            }
        });
    }

    public void setMessageTitle(String message) {
        titleTv.setText(message);
    }

    public void setLeftText(String message) {
        if (!TextUtils.isEmpty(message)) {
            leftTv.setText(message);
        }
    }

    public void setRightText(String message) {
        if (!TextUtils.isEmpty(message)) {
            rightTv.setText(message);
        }
    }

    public void setRightTvGone() {
        rightTv.setVisibility(View.GONE);
    }

    public void setTitleVisible(int visible) {
        titleTv.setVisibility(visible);
    }

    public void setEtInputHint(String hint) {
        etInput.setHint(hint);
    }

    public void setEtInputHint(int hint) {
        etInput.setHint(hint);
    }

    public String getText() {
        if (etInput != null) {
            return etInput.getText().toString().trim();
        } else {
            return "";
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (!hasFocus) {
            dismiss();
        }
    }

    public static class Builder {
        EditTextDialog mEditTextDialog;

        public Builder(Context context, int theme, String tvTitle) {
            mEditTextDialog = new EditTextDialog(context, theme, tvTitle);
        }

        public Builder(Context context, String tvTitle) {
            this(context, R.style.CustomDialog, tvTitle);
        }

        public EditTextDialog build() {
            return mEditTextDialog;
        }
    }


}
